package com.spring.controller.work.order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.spring.controller.ApiModel;
import com.spring.controller.BaseController;
import com.spring.controller.dto.order.Pojo;
import com.spring.controller.dto.order.User;
import com.spring.controller.dto.order.UserListForm;
import com.spring.controller.web.UserThreadLocal;

/**
 * SpringMVC接收复杂集合参数 (test)
 * 
 * Copyright 2016 CoderDream's Studio
 *	
 * All right reserved.
 *
 * Created on 2016年7月27日 下午3:54:12
 */
@SuppressWarnings("rawtypes")
@Controller
public class CatalogController extends BaseController{
	
	@RequestMapping
    @ResponseBody  
    public ApiModel processSubmit(@ModelAttribute( "pojo" ) Pojo pojo, @ModelAttribute( "user" ) User user) {  
//		User user = UserThreadLocal.get();	//从本地线程中获取User对象
		
		System.out.println("pojo.a:"+pojo.getA()+"  pojo.b :"+pojo.getB());  
	    System.out.println("user.name:"+user.getName()+"  user.pwd :"+user.getPwd());  
	 
	    Map<String, Object> map = new HashMap<>();
	    map.put("user", user);
	    map.put("pojo", pojo);
	    
	    return buildSuccMsg(map);
    }  
	
	
	@RequestMapping
    @ResponseBody  
    public ApiModel processSubmit1(@RequestParam String name, @RequestParam String pwd) {  
		User user = new User(name, pwd);
		System.out.println(user.getName() + ", " + user.getPwd());
		
	    return buildSuccMsg(user);
    }  
	
	@RequestMapping
	public ModelAndView preCata(){
		return new ModelAndView("/catalog/cata");
	}
	
/*	@RequestMapping 
    @ResponseBody  
    public ApiModel deleteCatalogSchemes(@RequestParam("idList[]") List<String> idList, Boolean isBatch) {  
        System.out.println(isBatch);
		
		return buildSuccMsg(idList); 
    }  */
	
	@RequestMapping
	public ModelAndView testUserList(@ModelAttribute UserListForm userForm) {
	    for (User user : userForm.getUsers()) {
	        System.out.println(user.getName() + " - " + user.getPwd());
	    }
	/*    
	    List list = new ArrayList();
	    for(int i=0;i<10;i++){
	     User user = new User(i+"","张"+i);
	     list.add(user);
	    }*/
	    
	    return new ModelAndView("/catalog/success");//.addObject("users", userForm.getUsers());
	}
	
	
	/**
	 * 模拟订单生成接收参数方法
	 * 
	 * 以下以postman的方式提交结果异常，看来只能通过form表单提交才能封装成对象……
	 * 
	 * 第一个参数：
	 * {users:[{"name":"chenhs","pwd":"123"},{"name":"qinjr","pwd":"456"}]}
	 * 第二个参数：
	 * {pojo:{"a":"1","b":"110110"}}
	 * 第三个参数【按照正常的GET/POST赋值方式】：
	 * actId = 110
	 * 
	 * @param userForm
	 * @return
	 */
	@RequestMapping
	@ResponseBody  
    public ApiModel testUserList1(@ModelAttribute UserListForm userForm) {
	    for (User user : userForm.getUsers()) {
	        System.out.println(user.getName() + " - " + user.getPwd());
	    }
	    
	    Pojo p = userForm.getPojo();
	    System.out.println(p.toString());
	    
	    return buildSuccMsg(userForm); 
	}


	@RequestMapping
	@ResponseBody
	public ApiModel testUserList2(@RequestBody UserListForm userForm) {
		for (User user : userForm.getUsers()) {
			System.out.println(user.getName() + " - " + user.getPwd());
		}

		Pojo p = userForm.getPojo();
		System.out.println(p.toString());

		return buildSuccMsg(userForm);
	}

}
